package com.tianji.learning.listener;

import com.tianji.common.constants.MqConstants;
import com.tianji.common.domain.dto.LikedTimesDTO;
import com.tianji.learning.domain.po.InteractionReply;
import com.tianji.learning.service.IInteractionReplyService;
import org.springframework.amqp.core.ExchangeTypes;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

@Component
public class ReplyLikedListener {

    @Autowired
    private IInteractionReplyService replyService;

    @RabbitListener(bindings = @QueueBinding(
            value  = @Queue(name = MqConstants.Queue.QA_LIKED_TIMES_QUEUE),
            exchange = @Exchange(name=MqConstants.Exchange.LIKE_RECORD_EXCHANGE, type = ExchangeTypes.TOPIC),
            key = MqConstants.Key.QA_LIKED_TIMES_KEY
    ))
    public void onReplyLiked(List<LikedTimesDTO> dtoList){
        List<InteractionReply> replyList = new ArrayList<>();
        for (LikedTimesDTO dto : dtoList) {
            InteractionReply reply = new InteractionReply();
            reply.setId(dto.getBizId());
            reply.setLikedTimes(dto.getLikedTimes()<=0?0:dto.getLikedTimes());
            reply.setUpdateTime(LocalDateTime.now());
            replyList.add(reply);
        }
        replyService.updateBatchById(replyList);
//        for (LikedTimesDTO dto : dtoList) {
//            //根据id更新点赞数量
//            if(dto.getLikedTimes() <= 0 ){
//                dto.setLikedTimes(0);
//            }
//            replyService.lambdaUpdate()
//                    .set(InteractionReply::getLikedTimes, dto.getLikedTimes())
//                    .eq(InteractionReply::getId, dto.getBizId())
//                    .update();
//        }
    }
}
